package com.audaque.springboot.foshanupload.web.springsecuritycas.config;

import com.audaque.springboot.foshanupload.core.constants.ComConst;
import org.jasig.cas.client.util.AssertionThreadLocalFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zgb
 * @desc ...
 * @date 2022-12-16 22:20:38
 */
@Configuration
public class CasUserFunctionFiterConfig {


    /**
     * 请求包装过滤器
     * 作用其实很简单就是 在HttpServletRequest对象在包装一次，让其支持getUserPrincipal，getRemoteUser方法来获取登录的用户信息。
     * wraper过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean filterWrapperRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new HttpServletRequestWrapperFilter());
        // 设定匹配的路径
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        // 设定加载的顺序
        registration.setOrder(4);
        return registration;
    }

    /**
     * 上下文包装过滤器
     * 使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。比如AssertionHolder.getAssertion().getPrincipal().getName()。
     * @return
     */
    @Bean
    public FilterRegistrationBean filterAssertionThreadLocalRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AssertionThreadLocalFilter());
        // 设定匹配的路径
        registration.addUrlPatterns(ComConst.FILTER_ANY_MATCH);
        // 设定加载的顺序
        registration.setOrder(5);
        return registration;
    }

}
